let Controller = {
	mounted() {
		this.initData();
	},
	activated() {
		this.initData();
		this.requestDetail();
		this.onDownloadExcel();
	},
	methods: {
		initData() {
			const param = JSON.parse(this.$route.query.data);
			this.Model.queryData = _.cloneDeep(param.row);
			this.Model.detailFlag = Boolean(param.flag);
			this.Model.status = param.row.status;

			this.Model.filterOption.items[2].type = this.Model.detailFlag ? 'text' : 'dropdown';
		},
		requestDetail() {
			const number = this.Model.queryData.number;
			if (number) {
				const param = {
					condition: {
						number
					}
				}
				this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/outAndInDetails`, param)
					.then(response => {
						const data = response.data;
						if (data.status == 200) {
							const result = data.result;
							this.Model.shouldSaleGoodsInfoVos = result.shouldSaleGoodsInfoVos;
							this.Model.actualSaleGoodsInfoVos = result.status !== '待入库' ? result.shouldSaleGoodsInfoVos : [];
							this.Model.whWarehouseId = result.whWarehouseId;
							this.Model.tabFormInline = [{
									label: '采购入库单号',
									value: result.number,
								},
								{
									label: '单据状态',
									value: result.status,
								},
							];
							this.setFilterData(result);
						}else{
							this.$message.error(data.msg)
						}
					})
					.catch(error => {
						this.$message.error('请求错误');
					});
			}
		},
		onDownloadExcel() {
			this.$ajax.get(this.apiTest + `/supplier/whWarehouseDetail/getGoodsExcelInfo`)
				.then(res => {
					const data = res.data;
					if (data.status == 200) {
						this.Model.fileUrl = data.result.fileUrl;
						this.Model.fileName = data.result.fileName;
					}
				})
		},
		onExportMaterial() {
			this.Model.dialogFormVisible = true;
		},
		onSureDelivery() {
			const list = this.Model.actualSaleGoodsInfoVos;
			if (list && list.length > 0) {
				const whWarehouseDetailIds = list.map(item => {
					return item.whWarehouseDetailId
				})
				const param = {
					whWarehouseDetailIds,
					whWarehouseId: this.Model.whWarehouseId, //售后入库id
					locationId: 10017, //入库仓库id 默认写死10017
					// "locationId": this.Model.filterOption.items[3].value, //入库仓库id 默认写死10017
				};
				this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/inWarehouse`, param)
					.then(res => {
						const data = res.data;
						if (data.status == 200) {
							this.$message({
								type: 'success',
								message: '已成功'
							})
						} else {
							this.$message.error(data.msg)
						}
					}).catch(err => {

					})
			}
		},
		onUploadSureFile() {
			/*  */
			const file = this.Model.uploadFile;
			const number = this.Model.queryData.number;
			let fileName;
			if (file && !file.name) {
				this.$message({
					type: 'error',
					message: '还没有选择要上传的文件!'
				});
				return;
			} else {
				const extension = file.name.split('.')[1] === 'xls';
				const extension2 = file.name.split('.')[1] === 'xlsx';
				const isLt2M = file.size / 1024 / 1024 < 5
				if (!extension && !extension2) {
					this.$message.warning('上传模板只能是 xls、xlsx格式!')
					return;
				}
				fileName = file.name;
			}
			let fileFormData = new FormData();
			fileFormData.append('file', file, fileName); //filename是键，file是值，就是要传的文件，test.zip是要传的文件名
			fileFormData.append('number', number); //filename是键，file是值，就是要传的文件，test.zip是要传的文件名
			let requestConfig = {
				headers: {
					'Content-Type': 'multipart/form-data'
				},
			}
			this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/importStorageWarehouseExcel`, fileFormData,
					requestConfig)
				.then(res => {
					const data = res.data;
					if (data.status == 200) {
						this.$message({
							message: '上传成功',
							type: 'success',
						});
						this.Modelx.actualSaleGoodsInfoVos = data.result;
						this.Model.dialogFormVisible = false;
					} else {
						this.$message.error(data.msg)
					}
				})
		},
		onUploadFileChange(e) {
			if (e.target.files.length == 0) {
				return;
			}
			this.Model.uploadFile = e.target.files[0];
		},
		requestSaleGoodsInfoByEntity() {
			const batchNo = this.Model.exportBarcode;
			const whWarehouseId = this.Model.whWarehouseId;
			const aList = this.Model.actualSaleGoodsInfoVos;
			const sList = this.Model.shouldSaleGoodsInfoVos;
			let listArr=[];
			for(let i =0;i<aList.length;i++){
				if(batchNo == aList[i].batchNo){
					this.$message({
						message: '已存在该物料',
						type: 'warning',
					});
					return;
				};
			}
			for (let j = 0; j < sList.length; j++) {
				if(batchNo == sList[j].batchNo){
					listArr.push(sList[j]);
				}
			};
			listArr = aList.concat(listArr);
			this.Model.actualSaleGoodsInfoVos = listArr;
		},
		setInWarehouseData(data) {
			const opt = data.map(item => {
				return {
					label: `${item.theNameOfThePlace} - ${item.descriptionOfWarehouseLocation}`,
					value: item.id
				}
			});
			return opt;
		},
		setFilterData(result) {
			const data = result;
			const inWarehouseName = this.Model.detailFlag ? data.inWarehouseName : this.setInWarehouseData(data.supplierFactoryLocationList);
			this.Model.filterOption.items = [{
					label: "入库单类型:",
					type: "text",
					data: data.type,
					placeholder: "请选择",
					value: undefined,
					key: "type"
				},
				{
					label: "供应商:",
					type: "text",
					data: data.supplierName,
					placeholder: "请选择",
					value: undefined,
					key: "supplierName"
				},
				{
					label: "入库仓库:",
					type: this.Model.detailFlag ? "text" : "dropdown",
					data: inWarehouseName,
					placeholder: "",
					value: data.inWarehouseName,
					key: "inWarehouseName"
				},
				{
					label: "操作人:",
					type: "text",
					data: data.operatorName,
					placeholder: "请选择",
					value: undefined,
					key: "operatorName"
				},
				{
					label: "创建人:",
					type: "text",
					data: data.createName,
					placeholder: "请选择",
					value: undefined,
					key: "createName"
				},
				{
					label: "备注:",
					type: "text",
					data: data.remark,
					placeholder: "",
					value: undefined,
					key: "remark"
				},
			];
		},
		toBarcodeDetail(row) {
			//跳转条码详情
			const name = 'purchaseWarehousingBarcodeDetail';
			this.$router.push({
				name: name,
				query: {
					data: row
				}
			})
		},
		setStepData(list, status) {
			let activeStep = 0,
				step = [];
			for (let i = 0; i < list.length; i++) {
				if (list[i].code == status) activeStep = i;
				step.push({
					title: list[i].codeName,
					description: list[i].createTime
				})
			};
			this.Model.stepOption = {
				activeStep,
				step
			}
		},
		getFilterData(val, item, option) {
			/* 
			 saleOrderStatus订单状态[1]
			 saleOrderTypes售中类型-状态联动[2][3]
			 saleOrderChannels销售渠道[4][5]
			 deliveryTypes发货方式[6]
			 */
			this.Model.filterOption.items = _.cloneDeep(option);
			/* switch (item.key) {
			  case 'saleOrderChannels':
			    this.Model.filterOption.items[5].data = this.Model.saleManList[item.val].map(obj => {
			      return {
			        label: obj.value,
			        value: obj.key
			      }
			    });
			    this.Model.filterOption.items[5].value = undefined;
			    break;
			  case 'saleOrderTypes':
			    this.Model.filterOption.items[3].data = this.Model.saleAfterOrderStatus[item.val].map(obj => {
			      return {
			        label: obj.value,
			        value: obj.key
			      }
			    });
			    this.Model.filterOption.items[3].value = undefined;
			    break;
			  default:
			    break;
			}; */
			this.Model.searchFromData = val;
		},
		onDeleteExportData() {
			/* const list = this.Model.exportTableSelectList.map(item => {
				return item.id
			});
			console.log(list) */
			const eList = this.Model.exportTableSelectList;
			if (eList.length == 0) return;
			const eLen = eList.length;
			let aList = _.cloneDeep(this.Model.actualSaleGoodsInfoVos);
			
			const aLen = aList.length;
			for (let i = 0; i < eLen; i++) {
			  for (let j = 0; j < aLen; j++) {
			    if (aList[j] !== undefined) {
			      if (eList[i].id == aList[j].id) {
			        aList[j] = undefined;
			      }
			    }
			
			  }
			};
			aList = aList.filter(a => a);
			this.$set(this.Model, 'actualSaleGoodsInfoVos', aList);
		},
		onTableSelectionChange(val) {
			this.Model.exportTableSelectList = val;
			console.log(this.Model.exportTableSelectList);
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = val;
		},
		toSearch(val) {
			console.log(val)
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
			console.log(val);
		},
		onTabChange(val) {
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === true) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = false;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = true;
			}
		},
		onDialogEdit(flag, val) {
			this.Model.editDialogFlag = flag;
			if (flag === false) return;
			const obj = this.Model[val];
			this.Model.editDialogFormData = _.cloneDeep(this.Model.defaultEditDialogFormData);
			this.getAddress();
		},
		changeAddress(val) {
			if (!val) {
				this.getAddress();
			}
		},
		getAddress() {
			const idcard = 4310555;
			this.$ajax.post(this.apiTest + `/sale/saleOrder/getAddressByIdCard/${idcard}`, {})
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.Model.addressList = data.result;
					}
				}).catch(err => {
					console.log(err)
				})
		},
		updateBaseSaleOrder() {
			const data = this.Model.editDialogFormData;
			let params;
			if (data.shippingMethod === '1') {
				params = {
					shippingMethod: data.shippingMethod,
					shipper: data.shipper,
					originChannel: data.originChannel,
					salesman: data.salesman,
				}
			} else {
				params = data;
			}
			this.$ajax.post(this.apiTest + `/sale/saleOrder/updateBaseSaleOrder`, params)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.onDialogEdit(false);
						this.$message({
							type: 'success',
							message: '已成功保存'
						});
						this.requestDetail();
					} else {
						this.$message.error(data.msg);
					}
				}).catch(err => {
					this.$message.error(err);
					this.onDialogEdit(false);
				});
		},
	}
}
export default Controller;
